#include "Win32.h"

CWin32::CWin32(void)
{
}

CWin32::CWin32(int widith , int height ):
m_height(height), m_widith(widith)
{	
	m_isRunning = false ;
	m_isPaused = false ;
}

CWin32::~CWin32(void)
{
}

bool CWin32::init(HINSTANCE instanceHandle,int show)
{
	WNDCLASS wc ;
	
	wc.style = CS_HREDRAW | CS_VREDRAW ;
	wc.lpfnWndProc = ::WndProc ;
	wc.cbClsExtra = 0 ;
	wc.cbWndExtra = 0 ;
	wc.hInstance = instanceHandle ;
	wc.hIcon = ::LoadIcon(0,IDI_APPLICATION) ;
	wc.hCursor = ::LoadCursor(0,IDC_ARROW) ;
	wc.hbrBackground = static_cast<HBRUSH>(::GetStockObject(WHITE_BRUSH)) ;
	wc.lpszMenuName = 0 ;
	wc.lpszClassName = L"Test" ;

	m_hInstance = instanceHandle ;

	if ( ! ::RegisterClass(&wc) )
	{
		::MessageBox(0,L"RegisterClass Failed - File : Win32.cpp",0,0) ;
		return false ;
	}

	m_windowHandle = ::CreateWindow(
			L"Test",
			L"Test",
			WS_OVERLAPPEDWINDOW,
			CW_USEDEFAULT ,
			CW_USEDEFAULT ,
			m_widith,
			m_height ,
			0,
			0,
			instanceHandle,
			this);

	if (m_windowHandle == 0 )
	{
		::MessageBox(0,L"CreateWindow Failed - File : Win32.cpp",0,0) ;
		return false ;
	}

	::ShowWindow(m_windowHandle, SW_SHOW);
	::UpdateWindow(m_windowHandle);

	m_isRunning = true ;

	return true ;
}

LRESULT CWin32::msgPump(UINT msg, WPARAM wParam, LPARAM lParam)
{
	switch (msg)
	{
		// WM_CLOSE is sent when the user presses the 'X' button in the
		// caption bar menu.
		case WM_CLOSE:
			m_isRunning = false ;
			//DestroyWindow(m_windowHandle);
			return 0;

		// WM_DESTROY is sent when the window is being destroyed.
		case WM_DESTROY:
			m_isRunning = false ;
			return 0;

		case WM_QUIT:
			m_isRunning = false ;
			return 0 ;
	}
	return DefWindowProc(m_windowHandle, msg, wParam, lParam);
}

LRESULT CALLBACK WndProc(HWND hWnd , UINT msg , WPARAM wParam , LPARAM lParam )
{
	static CWin32* localApp = 0;

	switch( msg )
	{
		case WM_CREATE:
		{
			// Get the 'this' pointer we passed to CreateWindow via the lpParam parameter.
			CREATESTRUCT* cs = (CREATESTRUCT*)lParam;
			localApp = (CWin32*)cs->lpCreateParams;
			return 0;
		}
	}

	// Don't start processing messages until after WM_CREATE.
	if( localApp )
		return localApp->msgPump(msg, wParam, lParam);
	else
		return DefWindowProc(hWnd, msg, wParam, lParam);
}

int CWin32::run()
{
	MSG  msg = {0} ;
//   timer.start() ;

   //while(msg.message != WM_QUIT)
	while (m_isRunning)
	{
		// If there are Window messages then process them.
		if(PeekMessage( &msg, 0, 0, 0, PM_REMOVE ))
		{
			TranslateMessage( &msg );
			DispatchMessage( &msg );
		}
		// Otherwise, do animation/game stuff.
		else
		{	
			// If the application is paused then free some CPU 
			// cycles to other applications and then continue on
			// to the next frame.
			if(  m_isPaused )
			{
				Sleep(20);
				continue;
			}
		
			/*	if( !isDeviceLost() )
				{
					//Do game stuff
					//	timer.stop() ;
					//		updateScene(timer.elapsed());
					//		drawScene();
				} 
			*/
		}
	}

   return (int)msg.wParam;
}